const express = require("express")
const fs = require('fs')

const app = express()
const port = 1788

app.get('/api/getPdf', (req, res) => {
  const filePath = './test.pdf'
  const fileSteam = fs.createReadStream(filePath)

  res.setHeader('Content-Type', "application/pdf")
  res.setHeader('Content-Disposition', 'attachment; filename="test.pdf"');

  // 创建文件流并发送给前端
  const fileStream = fs.createReadStream(filePath);
  fileStream.pipe(res);
  // console.log(fileSteam)
  // res.send({
  //     data: fileSteam
  // })

  // 处理流错误
  fileStream.on('error', (err) => {
    console.error('Error reading file:', err);
    res.status(500).send('Error reading file');
  });
})

app.listen(port, () => {
  console.log(`Server liste port ${port}`)
})